package com.wkk.controller;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wkk.entity.Dati;
import com.wkk.entity.Xiangxingzonghe;
import com.wkk.service.DatiService;
import com.wkk.service.UserService;

@Controller
@CrossOrigin
@RequestMapping("/dati")
public class DatiController {
	@Resource
	private DatiService datiService;
	@Resource
	private UserService userService;

	@RequestMapping("/save")
	public int save(Integer user_id, Integer question_id, Integer agree) {
		Dati dati = new Dati();
		dati.setAgree(agree);
		dati.setQuestion_id(question_id);
		dati.setUser_id(user_id);
		int count = datiService.add(dati);
		return count;

	}

	@RequestMapping("/xiangxing")
	@ResponseBody
	public Map<String, Object> saveshuxing(HttpServletRequest req) {
		String ower_user_id = req.getParameter("user_id");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", ower_user_id);
		List<Xiangxingzonghe> datilist = datiService.list(map);
		int[] arrxiangxing = new int[datilist.size()];
		// agree负数处理，提取xiangxing数组和对应map
		for (int i = 0; i < datilist.size(); i++) {
			int agree = datilist.get(i).getAgree();
			int xiangxing = datilist.get(i).getYuan_xiangxing();
			if (agree < 0) {
				if (xiangxing >= 0 && xiangxing <= 75) {
					xiangxing = xiangxing + 75;

				} else if (xiangxing > 75 && xiangxing <= 150) {
					xiangxing = xiangxing - 75;
				}
				agree = -agree;
			}
			arrxiangxing[i] = xiangxing;
			map.put(String.valueOf(xiangxing), agree);
		}
		// 数组内从小到大排序
		for (int i = 0; i < arrxiangxing.length; i++) {
			for (int j = i + 1; j < arrxiangxing.length; j++) {
				if (arrxiangxing[i] > arrxiangxing[j]) {
					int temp = arrxiangxing[i];
					arrxiangxing[i] = arrxiangxing[j];
					arrxiangxing[j] = temp;
				}
			}
		}
		// 递归依次求数组前两个的对应agree的值影响下的新的xiangxing值
		while (arrxiangxing.length > 1) {
			int a = Integer.parseInt(map.get(String.valueOf(arrxiangxing[0])).toString());
			int b = Integer.parseInt(map.get(String.valueOf(arrxiangxing[1])).toString());
			double x = ((double) b) / (a + b);
			int last = (int) (arrxiangxing[0] + (arrxiangxing[1] - arrxiangxing[0]) * x);
			map.remove(String.valueOf(arrxiangxing[0]));
			map.remove(String.valueOf(arrxiangxing[1]));
			arrxiangxing[0] = last;
			arrxiangxing = ArrayUtils.remove(arrxiangxing, 1);
			if (map.size() == 1) {
				map.put("xiangxing", String.valueOf(last));
			} else {
				map.put(String.valueOf(last), (a + b) / 2);
			}
		}
		// 存进对应user的属性记录里
		int count = userService.update(map);
		map.put("count", count);
		return map;

	}

}
